//
//  UserUtils.m
//  ShenWeiRent
//
//  Created by iMac on 2019/3/11.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "UserUtils.h"
#import "ANNavigationController.h"
#import "ANLoginController.h"

@implementation UserUtils

+ (instancetype)shared
{
    static UserUtils *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (BOOL)isLogin
{
    if (self.session && self.session.length > 0) {
        return YES;
    }
    return NO;
}

- (void)saveUserData:(NSDictionary *)data
{
    NSString *session = data[@"sessionId"];
    [self savesession:session];
    NSDictionary *member = data[@"member"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"orgCode"] forKey:@"vipCode"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"orgId"] forKey:@"vipId"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"orgName"] forKey:@"orgName"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"memberKind"] forKey:@"vipKind"];
//    [[NSUserDefaults standardUserDefaults] setObject:member[@"orgName"] forKey:@"orgName"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"telephone"] forKey:@"telephone"];
    [[NSUserDefaults standardUserDefaults] setObject:member[@"typeName"] forKey:@"typeName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)updateUserData:(UserInfoEntity *)data
{
    [[NSUserDefaults standardUserDefaults] setObject:data.orgCode forKey:@"vipCode"];
    [[NSUserDefaults standardUserDefaults] setObject:data.orgId forKey:@"vipId"];
    [[NSUserDefaults standardUserDefaults] setObject:data.orgName forKey:@"orgName"];
    [[NSUserDefaults standardUserDefaults] setObject:data.memberKind forKey:@"vipKind"];
//    [[NSUserDefaults standardUserDefaults] setObject:data.orgName forKey:@"orgName"];
    [[NSUserDefaults standardUserDefaults] setObject:data.telephone forKey:@"telephone"];
    [[NSUserDefaults standardUserDefaults] setObject:data.typeName forKey:@"typeName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)clearData
{
    [self clearsession];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"vipCode"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"vipId"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"vipName"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"vipKind"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"orgName"];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"telephone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)nameString
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"orgName"];
}

- (BOOL)isMembership
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"vipKind"] intValue] == 2) {
        return YES;
    }
    return NO;
}

- (NSString *)vipId
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"vipId"];
}

- (NSString *)vipCode
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"vipCode"];
}

- (NSString *)orgName
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"orgName"];
}

- (void)savesession:(NSString *)session
{
    [[NSUserDefaults standardUserDefaults] setObject:session forKey:@"session"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)clearsession
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"session"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)session
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"session"];
}

- (void)saveTelString:(NSString *)telString
{
    [[NSUserDefaults standardUserDefaults] setObject:telString forKey:@"telephone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)clearTelString
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"telephone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)telString
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"telephone"];
}


+ (void)showLoginVc
{
    [[UIViewController currentTopViewController] presentViewController:[[ANNavigationController alloc] initWithRootViewController:[[ANLoginController alloc] init]] animated:YES completion:nil];
}

@end
